package com.ruoyi.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.client.CourseClient;
import com.ruoyi.client.UserClient;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.domain.vo.ReservationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.ReservationMapper;
import com.ruoyi.domain.Reservation;
import com.ruoyi.service.IReservationService;

/**
 * 预约Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-18
 */
@Service
public class ReservationServiceImpl implements IReservationService 
{
    @Autowired
    private ReservationMapper reservationMapper;

    @Autowired
    private CourseClient courseClient;

    @Autowired
    private UserClient userClient;

    /**
     * 查询预约
     * 
     * @param reservationId 预约主键
     * @return 预约
     */
    @Override
    public Reservation selectReservationByReservationId(Long reservationId)
    {
        return reservationMapper.selectReservationByReservationId(reservationId);
    }

    /**
     * 查询预约列表
     * 
     * @param reservation 预约
     * @return 预约
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约
     * 
     * @param reservation 预约
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        return reservationMapper.insertReservation(reservation);
    }

    /**
     * 修改预约
     * 
     * @param reservation 预约
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return reservationMapper.updateReservation(reservation);
    }

    /**
     * 批量删除预约
     * 
     * @param reservationIds 需要删除的预约主键
     * @return 结果
     */
    @Override
    public int deleteReservationByReservationIds(Long[] reservationIds)
    {
        return reservationMapper.deleteReservationByReservationIds(reservationIds);
    }

    /**
     * 删除预约信息
     * 
     * @param reservationId 预约主键
     * @return 结果
     */
    @Override
    public int deleteReservationByReservationId(Long reservationId)
    {
        return reservationMapper.deleteReservationByReservationId(reservationId);
    }

    @Override
    public List<ReservationVO> getWithCoachAndCourse(Integer userid) {
        List<Reservation> reservationList = reservationMapper.selByUserId(userid);
        List<ReservationVO> reservationVOList = BeanUtil.copyToList(reservationList, ReservationVO.class);

        R<List<Map<String,Object>>> r = courseClient.findALL();
        List<Map<String, Object>> courseList = r.getData();
        Map<String, Map<String, Object>> courseMap = courseList.stream()
                .collect(Collectors.toMap(map -> map.get("courseId").toString(), map -> map));

        //远程调用获取所有教练
        R<List<Map<String, Object>>> r1 = (R<List<Map<String, Object>>>) userClient.selAllCoach();
        List<Map<String, Object>> coachList = r1.getData();
        Map<String, Map<String, Object>> coachMap = coachList.stream()
                .collect(Collectors.toMap(map -> map.get("coachId").toString(), map -> map));

        //组装数据
        reservationVOList.forEach(reservationVO -> {
            Map<String, Object> course = courseMap.get(reservationVO.getCourseId().toString());
            Map<String, Object> coach = coachMap.get(reservationVO.getCoachId().toString());
            reservationVO.setCoachName((String) coach.get("coachName"));
            reservationVO.setCourseName((String) course.get("courseName"));
        });

        return reservationVOList;
    }
}
